﻿using SqlSugar;

namespace FileApi.common
{
    public static class SqlSugarContext
    {

        /// <summary>
        /// 获取连接实例
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <param name="dbName"></param>
        /// <returns></returns>
        public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration, string dbName)
        {
            SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
            {
                ConnectionString = configuration.GetConnectionString(dbName),
                DbType = DbType.SqlServer,// 切换数据库类型
                IsAutoCloseConnection = true,// 自动释放和关闭数据库连接，如果有事务则结束时关闭，否则每次操作后关闭（false 需要手动释放）
                InitKeyType = InitKeyType.Attribute// 从实体特性中读取主键自增列信息
            });
            services.AddSingleton<ISqlSugarClient>(sqlSugar);// 这边是 SqlSugarScope 用 AddSingleton
        }
    }
}
